<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户名称">
          <el-select v-model="queryParams.publisher" placeholder="请选择商户名称" clearable @keyup.enter.native="handleQuery">
            <el-option v-for="item in publisherList" :key="item.name" :label="item.name" :value="item.name"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['activity:InformationDelivery:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['activity:InformationDelivery:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['activity:InformationDelivery:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['activity:InformationDelivery:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="InformationDeliveryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id(主键)" align="center" prop="gpId" /> -->
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="发布内容" align="center" prop="content" />
      <el-table-column label="发布人" align="center" prop="publisher" />
      <el-table-column label="上/下架状态" align="center" prop="status">
        <template v-slot:default="scope">
          <span>{{ scope.row.status === 0 ? '上架' : '下架' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot:default="scope">
          <el-button size="mini" type="text" @click="handleQueryInfo(scope.row)"
            v-hasPermi="['activity:InformationDelivery:query']">详情</el-button>
          <el-button v-show="scope.row.status == 1" size="mini" type="text"
            @click="scope.row.status=0" v-hasPermi="['activity:InformationDelivery:edit']">上架</el-button>
          <el-button size="mini" type="text" v-show="scope.row.status == 0" @click="scope.row.status=1"
            v-hasPermi="['activity:InformationDelivery:edit']">下架</el-button>
          <el-button size="mini" type="text" v-show="scope.row.status == 1" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['activity:InformationDelivery:edit']">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改信息发布对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="预览图" prop="img">
          <image-upload v-model="form.img"/>
        </el-form-item>
        <el-form-item label="发布内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-drawer title="" :visible.sync="drawer" :with-header="false" size="60%">
      <div style="height: 100px; line-height: 100px; margin-left: 20px;">详情</div>
      <hr>
      <div class="body">
        <div class="body1">
          <div class="body11">
            标题：
          </div>
          <div class="body12">
            {{InformationDelivery.title}}
          </div>
        </div>
        <div class="body1">
          <div class="body11">
            发布人：
          </div>
          <div class="body12">
            {{InformationDelivery.publisher}}
          </div>
        </div>
        <div class="body1">
          <div class="body11">
            发布时间：
          </div>
          <div class="body12">
            {{InformationDelivery.time}}
          </div>
        </div>
        <div class="body1">
          <div class="body11">
            内容：
          </div>
          <div class="body12">
            {{InformationDelivery.content}}
          </div>
        </div>
        <div class="body1">
          <div class="body21">
            图片：
          </div>
          <div class="body22">
            <img src="../../../../public/images/2.png" alt="">
            <img src="../../../../public/images/2.png" alt="">
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listInformationDelivery, getInformationDelivery, delInformationDelivery, addInformationDelivery, updateInformationDelivery, listPublisher } from "@/api/activity/InformationDelivery";

export default {
  name: "InformationDelivery",
  data() {
    return {
      drawer: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息发布表格数据
      InformationDeliveryList: [],
      //信息详情
      InformationDelivery:[],
      //商户列表
      publisherList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        publisher: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //详情打开
    handleQueryInfo(row) {
      this.drawer = true;
      if (row.gpId === undefined) {
        console.error('Row ID is undefined.');
        return;
      }
      const gpId = row.gpId;
      getInformationDelivery(gpId).then(response => {
        this.InformationDelivery = response.data;
      })
    },
    /** 查询信息发布列表 */
    getList() {
      this.loading = true;
      listInformationDelivery(this.queryParams).then(response => {
        this.InformationDeliveryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listPublisher().then(response => {
        this.publisherList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gpId: null,
        title: null,
        publisher: null,
        time: null,
        img: null,
        content: null,
        originalPrice: null,
        presentPrice: null,
        endTime: null,
        telephone: null,
        note: null,
        status: null,
        deleted: null,
        moreOne: null,
        moreTwo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gpId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信息发布";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gpId = row.gpId || this.ids
      getInformationDelivery(gpId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信息发布";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gpId != null) {
            updateInformationDelivery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInformationDelivery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gpIds = row.gpId || this.ids;
      this.$modal.confirm('是否确认删除信息发布编号为"' + gpIds + '"的数据项？').then(function () {
        return delInformationDelivery(gpIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('activity/InformationDelivery/export', {
        ...this.queryParams
      }, `InformationDelivery_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.body{
  width: 80%;
  text-align: center;
  margin-left: 10%;
  margin-top: 10%;
  box-sizing: border-box;
}
.body1{
  display: flex;
  width: 100%;
  box-sizing: border-box;
}
.body11{
  border: 1px lightgray solid;
  width: 20%;
  height: 60px;
  line-height: 60px;
  background-color: #EFEFEF;
  box-sizing: border-box;
}
.body12{
  border: 1px lightgray solid;
  width: 80%;
  height: 60px;
  line-height: 60px;
  box-sizing: border-box;
}
.body21{
  border: 1px lightgray solid;
  width: 20%;
  height: 150px;
  line-height: 150px;
  background-color: #EFEFEF;
  box-sizing: border-box;
}
.body22{
  border: 1px lightgray solid;
  width: 80%;
  height: 150px;
  line-height: 150px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}
.body22>img{
  width: 150px;
  height: 100px;
  margin-right: 10px;
}
</style>
